package com.utils.wechat;

public class WxUtils {

   /* //获取微信权限
    public static String getAccessToke(String appid,String secret) throws IOException {
        String Url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
        Url=String.format(Url,appid,secret);
        JSONObject jsonObject=wxGet(Url);
        System.out.println(jsonObject.toJSONString());
        String access_token=jsonObject.getString("access_token");
        String errmsg="";
        if(access_token!=null){
            System.out.println(access_token);
            return access_token;
        }else{
            errmsg=jsonObject.getString("errmsg");
            return errmsg;
        }
    }

    //获取错误信息
    public static String getRidMsg(String rid,String token) throws IOException {
        String Url="https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token=%s";
        Url=String.format(Url,token);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("rid",rid);
        JSONObject res=wxPost(Url,jsonObject);
        return res.toJSONString();
    }

    //发送模板信息
    public static String postTemplateMsg(JSONObject templateMsg,String token) throws IOException {
        System.out.println("调用发送模板信息"+token);
        String Url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
        Url=String.format(Url,token);
        System.out.println(Url);
        JSONObject jsonObject=wxPost(Url,templateMsg);
        return jsonObject.toJSONString();
    }

    //无法使用，使用测试接口进行
    //https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
    public static String postMenuCreate(JSONObject menuMsg,String token) throws IOException {
        String Url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";
        Url=String.format(Url,token);
        JSONObject jsonObject=wxPost(Url,menuMsg);
        return jsonObject.toJSONString();
    }*/

    /*public static JSONObject wxGetUserInfo(String Url) throws IOException {
        CloseableHttpClient httpClient=HttpClients.createDefault();
        HttpGet httpGet=new HttpGet(Url);
        httpGet.setHeader("ContentType","application/json;charset=UTF-8");
        CloseableHttpResponse response=httpClient.execute(httpGet);
        HttpEntity entity=response.getEntity();
        String body =EntityUtils.toString(entity);
        JSONObject jsonObject=JSONObject.parseObject(body);
        response.close();
        httpClient.close();
        return jsonObject;
    }

    public static JSONObject wxGet(String Url) throws IOException {
        CloseableHttpClient httpClient=HttpClients.createDefault();
        HttpGet httpGet=new HttpGet(Url);
        CloseableHttpResponse response=httpClient.execute(httpGet);
        HttpEntity entity=response.getEntity();
        String body =EntityUtils.toString(entity);
        JSONObject jsonObject=JSONObject.parseObject(body);
        response.close();
        httpClient.close();
        return jsonObject;
    }

    public static JSONObject wxPost(String Url,JSONObject body) throws IOException {
        CloseableHttpClient httpClient=HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(Url);
        //转换为请求体注意转换
        StringEntity stringEntity=new StringEntity(body.toString(), Charset.forName("UTF8"));
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response=httpClient.execute(httpPost);
        HttpEntity httpEntity=response.getEntity();
        String res=EntityUtils.toString(httpEntity);
        JSONObject jsonObject=JSONObject.parseObject(res);
        return jsonObject;
    }*/
}
